import req from '@/utils/request'

export interface QuanGuoBaoQuanForm {
  excel: string
  file: string
  username: string
  password: string
}
export interface FlowPostItem {
  name: string
  options?: string[]
  title: string
  type: string
  value: unknown
  username_field?: string
  password_field?: string
}

export interface FlowPostGroup {
  title: string
  flows: FlowPost[]
}

export interface FlowPost {
  arguments: FlowPostItem[]
  description?: string
  name: string
  title: string
}

export interface FormType {
  [prop: string]: unknown
}

export interface FsItem {
  isDir: boolean
  name: string
  path: string
}

export interface DirResult {
  current: string
  list: Array<FsItem>
}

export interface UserResItem {
  显示名称: string
  密码: string
  用户名: string
  序号: number
}

export function flow(): Promise<FlowPostGroup[]> {
  // return req.get('/flow/')
  return pywebview.api.list_flow()
}

export function flowsubmit(
  flowname: string,
  form: FormType,
  execute_time?: number
): Promise<string> {
  // return req.post('/flow/submit', form)
  return pywebview.api.submit_flow(flowname, form, execute_time)
}

export function submit(data: QuanGuoBaoQuanForm): Promise<string> {
  const f = new FormData()
  f.append('excel', data.excel)
  f.append('file', data.file)
  f.append('username', data.username)
  f.append('password', data.password)
  return req.post('/submit', f)
}

export function baoquanSubmit(data: QuanGuoBaoQuanForm): Promise<string> {
  const f = new FormData()
  f.append('excel', data.excel)
  f.append('file', data.file)
  f.append('username', data.username)
  f.append('password', data.password)
  return req.post('/xj/submit', f)
}

export function chooseFile(): Promise<string[]> {
  // return req.post('/choosefile', {
  //   allowMultiple: false
  // })
  return pywebview.api.choose_file()
}

export function chooseDir(): Promise<string[]> {
  // return req.post('/choosedir', {})
  return pywebview.api.choose_dir()
}

export function getDir(path?: string): Promise<DirResult> {
  return req.post(`/choosedir${path ? '?base=' + path : ''}`)
}

export function getForwardDir(path: string, mid: string): Promise<DirResult> {
  return req.get(
    `/getForwardDir?base=${encodeURIComponent(path)}&mid=${encodeURIComponent(
      mid
    )}`
  )
}

// interface UserRespose {
//   result: UserResItem[]
// }

export function getUserData(): Promise<UserResItem[]> {
  // return req.get('/user/')
  return pywebview.api.list_user()
}
